<?php
/**
 * Created by PhpStorm.
 * User: sunfan
 * Date: 2017/8/16
 * Time: 10:11
 */

namespace App\Controller;


class IndexController extends ApiBaseController
{
    public function index(){
        $data = $this->data;
        $user_id = S($data['token'])?S($data['token']):0;
        // 返回数据
        $info = array();
        try{
            // banner
            $banner = D("Slide")
                ->where(array('slide_status'=>1))
                ->field('slide_id as id,slide_name,slide_url,slide_pic,slide_des')
                ->select();
            foreach($banner as $k => $v){
                $banner[$k]['slide_pic'] = "/data/upload/".$v['slide_pic'];
            }
            $info['slide'] = $banner;

            $ad = D("Ad")->where(array('status'=>1))->order('ad_id')->find();
            $info['ad'] = $ad;
            // 限时抢购
            $time = time();
            $sql['end_time'] = array('egt',$time);
            $promotion = D("Promotion")
                ->where($sql)
                ->field("id,start_time,end_time")
                ->select();

            foreach($promotion as  $p => $q){
                $promotion[$p]['start_time'] = date("Y-m-d H:i:s",$q['start_time']);
                $promotion[$p]['end_time'] = date("Y-m-d H:i:s",$q['end_time']);
            }

            $product = D("PromotionProduct")
                ->alias("m")
                ->join("cai_product as p on m.product_id = p.id")
                ->where(array('m.promotion_id'=>$promotion[0]['id'],'p.status'=>1))
                ->field("p.id,p.image,p.title,p.price_plus as price")
                ->select();
            if(!empty($product)){
                $promotion[0]['product'] = $product;
            }
            $info['promotion'] = $promotion;

            // 热卖专区
            $hot = D("Product")
                ->where(array('status'=>1,'hot'=>1))
                ->field("id,title,image,price")
                ->select();
            $info['hot'] = $hot;

            // 首页推荐
            $recommend = D("Product")
                ->where(array('status'=>1,'recommend'=>1))
                ->field("id,title,stock,start_num,step,image,price,desc,sold,bazar_price,attrs_key")
                ->select();
            if($recommend) {
                foreach ($recommend as $k => $v) {
                    if ($v['attrs_key']) {
                        $parameter = D("Parameter")
                            ->where(array('product_id' => $v['id']))
                            ->field("id,attr_title,attr_value,stock,price")
                            ->select();
                        foreach ($parameter as $p => $q) {
                            $cart = D("Cart")
                                ->where(array('user_id' => $user_id, 'product_id' => $v['id'], 'attr_id' => $q['id']))
                                ->field('id,product_sum,attr_id')
                                ->find();
                            $parameter[$p]['cart'] = $cart ? $cart : array();
                        }
                        $recommend[$k]['parameter'] = $parameter;
                    }else{
                        $recommend[$k]['parameter'] = array();
                        $cart = D("Cart")
                            ->where(array('user_id' => $user_id, 'product_id' => $v['id']))
                            ->field('id,product_sum,attr_id')
                            ->find();
                        $recommend[$k]['cart'] =  $cart?$cart:array();
                    }
                }
            }
            $info['recommend'] = $recommend;
            $code = 1;
            $msg = "成功";
        }catch (\Exception $e){
           $code = -1;
           $msg = "系统错误";
        }
        $this->ApiReturn($code,$msg,$info);
    }
    /// 搜索
    public function search(){
        $data = $this->data;
        $user_id = S($data['token'])?S($data['token']):0;
        $keyword = $data['keyword'];
        $where = array();
        $where['title'] = array('like',"%{$keyword}%");
        $where['desc'] = array('like',"%{$keyword}%");
        $where['_logic'] = "OR";
        $page = $data['page']?$data['page']:1;
        try{
            $list = D("Product")
                ->where($where)
                ->field("id,title,price,stock,start_num,step,image,desc,bazar_price,attrs_key")
                ->order('create_time desc')
                ->limit(($page-1)*10,10)
                ->select();
           $code = 0;
           $msg = "ok";
           if($list){
               foreach($list as $k => $v) {
                  if($v['attrs_key']){
                      $parameter  = D("Parameter")
                          ->where(array('product_id' => $v['id']))
                          ->field("id,attr_title,attr_value,stock,price")
                          ->select();
                      foreach($parameter as $p => $q){
                          $cart = D("Cart")
                              ->where(array('user_id' => $user_id, 'product_id' => $v['id'],'attr_id'=>$q['id']))
                              ->field('id,product_sum,attr_id')
                              ->find();
                          $parameter[$p]['cart'] = $cart?$cart:array();
                      }
                      $list[$k]['parameter'] = $parameter;
                  }else{
                       $list[$k]['parameter'] = array();
                       $cart = D("Cart")
                           ->where(array('user_id' => $user_id, 'product_id' => $v['id']))
                           ->field('id,product_sum,attr_id')
                           ->find();
                       $list[$k]['cart'] =  $cart?$cart:array();
                  }
               }
               $code = 1;
           }
        }catch (\Exception $e){
            $msg = "系统错误";
        }
        $this->ApiReturn($code,$msg,$list);
    }
    // 抢购商品
    public function inPromotion(){
        $data = $this->data;
        $id = $data['id'];
        try{
            $code = -1;
            $msg = "确实参数 id";
            if($id){
                $code = 0;
                $msg = "成功";
                $product = D("PromotionProduct")
                    ->alias("m")
                    ->join("cai_product as p on m.product_id = p.id")
                    ->where(array('m.promotion_id'=>$id,'p.status'=>1))
                    ->field("p.image,p.title,p.price_plus as price")
                    ->select();
                $product;
                if($product){
                    $code = 1;
                }
            }
        }catch (\Exception $e){
            $code = -1;
            $msg =  "系统错误";
        }
        $this->ApiReturn($code,$msg,$product);
    }
    //热卖专区
    public function hot_product(){
        $data = $this->data;
        $user_id = S($data['token'])?S($data['token']):0;
//        $keyword = $data['keyword'];
        $where = array();
        $where['hot'] = 1;
//        $where['title'] = array('like',"%{$keyword}%");
//        $where['desc'] = array('like',"%{$keyword}%");
//        $where['_logic'] = "OR";
        $page = $data['page']?$data['page']:1;
        try{
            $list = D("Product")
                ->where($where)
                ->field("id,title,price,stock,start_num,step,image,desc,bazar_price,attrs_key")
                ->order('create_time desc')
                ->limit(($page-1)*10,10)
                ->select();
            $code = 0;
            $msg = "ok";
            if($list){
                foreach($list as $k => $v) {
                    if($v['attrs_key']){
                        $parameter  = D("Parameter")
                            ->where(array('product_id' => $v['id']))
                            ->field("id,attr_title,attr_value,stock,price")
                            ->select();
                        foreach($parameter as $p => $q){
                            $cart = D("Cart")
                                ->where(array('user_id' => $user_id, 'product_id' => $v['id'],'attr_id'=>$q['id']))
                                ->field('id,product_sum,attr_id')
                                ->find();
                            $parameter[$p]['cart'] = $cart?$cart:array();
                        }
                        $list[$k]['parameter'] = $parameter;
                    }else{
                        $list[$k]['parameter'] = array();
                        $cart = D("Cart")
                            ->where(array('user_id' => $user_id, 'product_id' => $v['id']))
                            ->field('id,product_sum,attr_id')
                            ->find();
                        $list[$k]['cart'] =  $cart?$cart:array();
                    }
                }
                $code = 1;
            }
        }catch (\Exception $e){
            $msg = "系统错误";
        }
        $this->ApiReturn($code,$msg,$list);
    }

        //获取当前时间
    public function time(){
        try{
            $info = time();
            $code = 1;
            $msg = '成功';
        }catch(\Exception $e){
            $code = -1;
            $msg = '系统错误';
        }
        $this->ApiReturn($code,$msg,$info);
    }
























}